package com.prs.crm.dao.oa.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.MailBoxDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.MailBox;

public class MailBoxDaoImpl extends BaseDaoImpl<MailBox> implements MailBoxDao{

	public MailBox findByAddr(final Person owner,final String addr)
	{
		return (MailBox)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						@SuppressWarnings("unchecked")
						public Object doInHibernate(Session session) throws HibernateException {
						
					        	Criteria criteria = session.createCriteria(MailBox.class);
					        	criteria.add(Restrictions.eq("recorder", owner));
					        	criteria.add(Restrictions.eq("addr", addr));
					        	criteria.addOrder(Order.asc("id"));
					        	List<MailBox> result = criteria.list();
					        	if (result.size()>0)
					        		return result.get(0);
					        	else
					        		return null;
				        }   
				 } );
	}
	
	@SuppressWarnings("unchecked")
	public Collection<MailBox> findBoxes(final Person owner)
	{
		return (Collection<MailBox>)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
						
					        	Criteria criteria = session.createCriteria(MailBox.class);
					        	criteria.add(Restrictions.eq("recorder", owner));
					         	criteria.addOrder(Order.asc("id"));
					        	return  criteria.list();
				        }   
				 } );
	}
	
	@SuppressWarnings("unchecked")
	public Collection<MailBox> getAllBox()
	{
		return (Collection<MailBox>)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(MailBox.class);
				        	criteria.setFetchMode("recorder", FetchMode.JOIN);
				        	return criteria.list();
				        	
				        }   
				 } );
	}
	
	public MailBox findDefaultMailBox(final Person owner)
	{
		return (MailBox)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						@SuppressWarnings("unchecked")
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(MailBox.class);
				        	criteria.add(Restrictions.eq("recorder", owner));
				        	criteria.add(Restrictions.eq("defaultBox", Boolean.TRUE));
				        	List<MailBox> result = criteria.list();
				        	if (result.size()>0)
				        		return result.get(0);
				        	else
				        		return null;
				        }   
				 } );
	}
}
